"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.onTabItemTap = exports.onResize = exports.onPageScroll = exports.onAddToFavorites = exports.onShareTimeLine = exports.onShareAppMessage = exports.onReachBottom = exports.onPullDownRefresh = exports.onUnload = exports.onHide = exports.onReady = exports.onShow = exports.onLoad = exports.detached = exports.moved = exports.ready = exports.attached = exports.created = exports.createLifecycleMethods = exports.PageLifecycle = exports.ComponentLifecycle = void 0;
var utils_1 = require("./utils");
var shared_1 = require("./shared");
var instance_1 = require("./instance");
// properties（props传值）
// data（值）
// observers（字段监听器）
// methods(方法)
// 组件的钩子函数
var ComponentLifecycle;
(function (ComponentLifecycle) {
    ComponentLifecycle["CREATED"] = "created";
    ComponentLifecycle["ATTACHED"] = "attached";
    ComponentLifecycle["READY"] = "ready";
    ComponentLifecycle["MOVED"] = "moved";
    ComponentLifecycle["DETACHED"] = "detached";
    // show
    // hide
    // resize
    // properties
    // data
    // observers
    // methods
    // options
    // behaviors
    // relations
    // externalClasses
    // lifetimes
    // pageLifetimes
    // definitionFilter
})(ComponentLifecycle = exports.ComponentLifecycle || (exports.ComponentLifecycle = {}));
// 小程序的钩子函数
var PageLifecycle;
(function (PageLifecycle) {
    PageLifecycle["LOAD"] = "onLoad";
    PageLifecycle["SHOW"] = "onShow";
    PageLifecycle["READY"] = "onReady";
    PageLifecycle["HIDE"] = "onHide";
    PageLifecycle["UNLOAD"] = "onUnload";
    PageLifecycle["PULL_DOWN_REFRESH"] = "onPullDownRefresh";
    PageLifecycle["REACH_BOTTOM"] = "onReachBottom";
    PageLifecycle["SHARE_APP_MESSAGE"] = "onShareAppMessage";
    PageLifecycle["SHARE_TIME_LINE"] = "onShareTimeLine";
    PageLifecycle["ADD_TO_FAVORITES"] = "onAddToFavorites";
    PageLifecycle["PAGE_SCROLL"] = "onPageScroll";
    PageLifecycle["RESIZE"] = "onResize";
    PageLifecycle["TAB_ITEM_TAP"] = "onTabItemTap";
})(PageLifecycle = exports.PageLifecycle || (exports.PageLifecycle = {}));
/**
 * 创建钩子
 * @param lifecycle {PageLifecycle|ComponentLifecycle} 生命周期
 * @return {Function} 返回钩子函数
 */
function createHook(lifecycle) {
    return function (callback) {
        var currentInstance = instance_1.getCurrentInstance();
        currentInstance && injectHook.call(currentInstance, lifecycle, callback);
    };
}
/**
 * 注入 hooks
 * @param lifecycle {PageLifecycle|ComponentLifecycle} 生命周期
 * @param hook {Function} 注入的hook
 */
function injectHook(lifecycle, hook) {
    var privateName = shared_1.createPrivateName(lifecycle);
    this[privateName] === undefined && (this[privateName] = []);
    this[privateName].push(hook); // 添加hook
}
/**
 * 执行 hooks
 * @param lifecycle {PageLifecycle|ComponentLifecycle} 生命周期
 * @param params {any[]} 执行的参数
 * @return {any[]} 所有方法执行返回的值
 */
function conductHook(lifecycle, params) {
    var _this = this;
    var privateName = shared_1.createPrivateName(lifecycle);
    var injectLifes = this[privateName] || [];
    return injectLifes.map(function (life) { return utils_1.isFunction(life) && life.apply(_this, params); });
}
/**
 * 创建生命周期
 * @param lifecycle {PageLifecycle|ComponentLifecycle} 生命周期
 * @param options {PlainObject} 参数
 * @return {Function} 返回生命周期执行函数
 */
function createLifecycleMethods(lifecycle, options) {
    var lifecycleMethod = options[lifecycle]; // options中的生命周期
    return function () {
        var args = [];
        for (var _i = 0; _i < arguments.length; _i++) {
            args[_i] = arguments[_i];
        }
        var injectLifes = conductHook.call(this, lifecycle, args); // 执行hooks
        lifecycleMethod && injectLifes.push(lifecycleMethod.apply(this, args));
        // 返回值，适应分享等钩子
        return injectLifes;
    };
}
exports.createLifecycleMethods = createLifecycleMethods;
// 组件钩子
exports.created = createHook(ComponentLifecycle.CREATED);
exports.attached = createHook(ComponentLifecycle.ATTACHED);
exports.ready = createHook(ComponentLifecycle.READY);
exports.moved = createHook(ComponentLifecycle.MOVED);
exports.detached = createHook(ComponentLifecycle.DETACHED);
// 页面钩子
exports.onLoad = createHook(PageLifecycle.LOAD);
exports.onShow = createHook(PageLifecycle.SHOW);
exports.onReady = createHook(PageLifecycle.READY);
exports.onHide = createHook(PageLifecycle.HIDE);
exports.onUnload = createHook(PageLifecycle.UNLOAD);
exports.onPullDownRefresh = createHook(PageLifecycle.PULL_DOWN_REFRESH);
exports.onReachBottom = createHook(PageLifecycle.REACH_BOTTOM);
exports.onShareAppMessage = createHook(PageLifecycle.SHARE_APP_MESSAGE);
exports.onShareTimeLine = createHook(PageLifecycle.SHARE_TIME_LINE);
exports.onAddToFavorites = createHook(PageLifecycle.ADD_TO_FAVORITES);
exports.onPageScroll = createHook(PageLifecycle.PAGE_SCROLL);
exports.onResize = createHook(PageLifecycle.RESIZE);
exports.onTabItemTap = createHook(PageLifecycle.TAB_ITEM_TAP);
